<?php
namespace app\operator\controller;
use think\Db;
use think\Validate;
use mlsmt\Cabinet\Send;
class Charecabinet extends Common
{
	//机柜列表
    public function binding()
    {
    	$cabinet_id  = input('param.cabinet_id');
    	$sid  = input('param.sid');
        $oid  = input('param.oid');
        $cover  = input('param.cover');
        $openid = mwdecrypt(input('param.openid'));

        $seller = Db::name('seller')->where(['id'=>$sid,'is_delete'=>1])->field('id,name')->find();
        if(!$seller){
            $info['code'] = 0;
            $info['msg'] = '绑定商户错误！';
            return  $info;
        }
        $charecabinet = Db::name('charecabinet')->where(['cabinet_id'=>$cabinet_id,'is_delete'=>1])->field('id,oid,sid')->find();
        if(!$charecabinet){
            $info['code'] = 0;
            $info['msg'] = '请先到PC后台添加此机柜信息！';
            return  $info;
        }
        if($charecabinet['oid']!=$oid){
            $info['code'] = 0;
            $info['msg'] = '此机柜不属于贵公司！';
            return  $info;
        }
        if($charecabinet['sid']==$sid){
            $info['code'] = 0;
            $info['msg'] = '此机柜已经绑定了此商户了！';
            return  $info;
        }
        if($charecabinet['sid']&&$cover==false){
            $info['code'] = 2;
            $info['msg'] = '此机柜已经绑定了别的商户了，确定要重新绑定到该商户吗？';
            return  $info;
        }else{
            $status = Db::name('charecabinet')->where(['cabinet_id'=>$cabinet_id,'is_delete'=>1])->update(['sid' => $sid ]);
            if($status){
                $info['code'] = 1;
                $info['msg'] = '绑定成功！';
                return  $info;
            }else{
                $info['code'] = 0;
                $info['msg'] = '绑定失败！';
                return  $info;
            }
            
        }
    }
    //获取机柜ID
    public function getid(){
        $cabinet_id = input('param.cabinet_id');
        $oid = input('param.oid');
        $openid = mwdecrypt(input('param.openid'));
        $charecabinet = Db::name('charecabinet')->where(['cabinet_id'=>$cabinet_id,'is_delete'=>1])->field('id')->find();
        if($charecabinet){
            $info['code'] = 1;
            $info['id'] = $charecabinet['id'];
            $info['msg'] = '查询成功！';
            return  $info;
        }else{
            $info['code'] = 0;
            $info['msg'] = '机柜不存在！';
            return  $info;
        }
    }
    //机柜详情
    public function info()
    {
        
        $cid = input('param.cid');
        $oid = input('param.oid');
     	$openid = mwdecrypt(input('param.openid'));
     	$charecabinet = Db::name('charecabinet')->where(['id'=>$cid,'is_delete'=>1])->find();
     	if(!$charecabinet||!$charecabinet['cabinet_id']){
     		$info['code'] = 0;
     		$info['msg'] = '机柜不存在！';
     		return  $info;
     	}
        $Send = new Send($charecabinet['cabinet_id']);
        $detail = $Send->StockDetail();

        if($detail['types']!=1){
     		$info['code'] = 0;
     		$info['msg'] = '查询状态失败！';
     		return  $info;
     	}
        
     	$charecabinet['detail'] = $detail['info'];
     	for($i=1;$i<11;$i++){

     		if(!isset($charecabinet['detail'][$i])){
     			$charecabinet['detail'][$i]='';
     		}else{
     			$charecabinet['detail'][$i]['lockid'] = $i;
     		}
     	}

     	$info['code'] = 1;
     	$info['charecabinet'] = $charecabinet;
     	return  $info;

    }
    //弹出充电宝
    public function ejectdevicee ()
    {
        $cabinet_id = input('param.cabinet_id');
        $lockid = input('param.lockid');
     	$openid = mwdecrypt(input('param.openid'));
        $oid = input('param.oid');
        $Send = new Send($cabinet_id);
        $detail = $Send->EjectDevice($lockid);

        $detail['types']==1?$info['code'] = 1:$info['code'] = 0;
        return  $info;

    }
    //重启
    public function restart ()
    {
        $cabinet_id = input('param.cabinet_id');
        $openid = mwdecrypt(input('param.openid'));
        $oid = input('param.oid');
        $Send = new Send($cabinet_id);
        $res = $Send->Restart();

        if($res['types']==1){
            $info['code']=1;
            return  $info;
        }else{
            $info['code']=0;
            $info['info']=$res['info'];
            return  $info; 
        }
        

    }
    //查询配置
    public function configure(){
        $cabinet_id = input('param.cabinet_id');
        $openid = mwdecrypt(input('param.openid'));
        $oid = input('param.oid');
        $Send = new Send($cabinet_id);
        $configure = $Send->GetConfigure();

        if($configure['types']==1){
            
            $version = $Send->Version($cabinet_id);

            if($configure['types']==1){
                
                $info['configure'] =$configure['info'];
                $info['configure']['version'] =$version['info'];
                $info['code']=1;
                return  $info;

            }

            
        }

        $info['code']=0;

        return  $info;
       
    }  
    //更新配置文件 
    public function update(){
        $data =  input('param.');
        $cabinet_id =  $data['cabinet_id'];
        $openid = mwdecrypt($data['openid']);
        $update = $data['update'];
        $oid = $data['oid'];
        $Send = new Send($cabinet_id);
        $configure = $Send->UpdateStatus($update['servicer'],$update['port'],$update['heartbeat']);
        if($configure['types']==1){

            $info['code']=1;
            return  $info;
            
        }else{
            $info['code']=0;
            return  $info;
        }

        

    }
    //获取机柜升级包
    public function getversion(){
        $version = Db::name('charecabinet_version')->where('is_delete',1)->select();
        return  $version;
    }
    //升级 
    public function ota(){
        $cabinet_id =  input('param.cabinet_id'); 
        $openid = mwdecrypt(input('param.openid')); 
        $filename = input('param.filename'); 
        $oid = input('param.oid');
        $ftpaddr = config('ftp.addr');
        $ftpusername = config('ftp.username');
        $ftppassword =config('ftp.password');
        $ftpport= config('ftp.port');

        $Send = new Send($cabinet_id);
        $configure = $Send->RemoteUpdate($ftpaddr,$ftpport,$filename,$ftpusername,$ftppassword);

        if($configure['types']==1){
            
            $info['code']=1;
            return  $info;
            
        }else{
            $info['code']=0;
            return  $info;
        }

        

    }
    //获取系统信息
    public function getsysteminfo(){
        $sid  = input('param.sid');
        $info['systeminfo'] = Db::name('systeminfo')->where('id',1)->find();
        $info['seller'] = Db::name('seller')->where(['id'=>$sid,'is_delete'=>1])->field('id,name')->find();
        if($info['systeminfo']['logo']){
            $info['systeminfo']['logo'] = config('website').$info['systeminfo']['logo'];
        }
        if(!$info['seller'] ){
            $info['code'] = 0;
            $info['msg'] = '商户信息错误！';
            return  $info;
        }else{
            $info['code'] = 1;
            return  $info;
        }
    }



}
